﻿using System.Collections.Generic;
using WebSelfHost.Logic.Sys;
using WebSelfHost.Entity.Sys;
using WebSelfHost.Utility.ResponseModels;
using WebSelfHost.Utility.Core;
using WebSelfHost.Web.Filters;
using WebSelfHost.Logic;
using WebSelfHost.Utility.Operator;
using Microsoft.AspNetCore.Mvc;
using WebSelfHost.Entity.WebRequestParms.Sys.Organize;
using WebSelfHost.Entity.WebRequestParms; 

#if NETFRAMEWORK
using System.Web.Http;
using OwinSessionMiddleware.WebApi;
#else
using Microsoft.AspNetCore.Http; 
#endif

namespace WebSelfHost.Web.Controllers.Sys
{
    [HiddenApi]
    public class OrganizeController : BaseController
    {
        private SysOrganizeLogic organizeLogic;

        public OrganizeController()
        {
            organizeLogic = new SysOrganizeLogic();
        }

        /// <summary>
        /// 组织机构主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/organize/index"), AuthorizeChecked]
        public ActionResult Index()
        {
            return Html("/Sys/Organize/Index");
        }

        /// <summary>
        /// 组织机构主界面数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/organize/index"), AuthorizeChecked]
        public ActionResult Index(OrganizeIndexParms parms)
        {
            int totalCount = 0;
            var pageData = organizeLogic.GetList(parms.pageIndex, parms.pageSize, parms.keyWord, ref totalCount);
            var result = new LayPadding<SysOrganize>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount//pageData.Count
            };
            return Content(result.ToJson());
        }

        /// <summary>
        /// 新增/修改组织机构主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/organize/form"), AuthorizeChecked]
        public ActionResult Form()
        {
            return Html("/Sys/Organize/Form");
        }

        /// <summary>
        /// 新增/修改组织机构数据提交
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("system/organize/form"), AuthorizeChecked]
        public ActionResult Form(SysOrganize model)
        {
            if (model.Id.IsNullOrEmpty())
            {
                int row = organizeLogic.Insert(model, OperatorProvider.Instance.GetCurrent(Request).Account);
                return row > 0 ? Success() : Error();
            }
            else
            {
                int row = organizeLogic.Update(model, OperatorProvider.Instance.GetCurrent(Request).Account);
                return row > 0 ? Success() : Error();
            }
        }

        /// <summary>
        /// 根据主键获取组织机构
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/organize/getForm"), LoginChecked]
        public ActionResult GetForm(PrimaryKeyParms parms)
        {
            var entity = organizeLogic.Get(parms.primaryKey);
            return Content(entity.ToJson());
        }

        /// <summary>
        /// 组织机构主界面删除方法
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/organize/delete"), AuthorizeChecked]
        public ActionResult Delete(PrimaryKeyParms parms)
        {
            int count = organizeLogic.GetChildCount(parms.primaryKey);
            if (count == 0)
            {
                int row = organizeLogic.Delete(parms.primaryKey);
                return row > 0 ? Success() : Error();
            }
            return Error(string.Format("操作失败，请先删除该项的{0}个子级机构。", count));
        }

        /// <summary>
        /// 组织机构主界面详细信息按钮方法
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/organize/detail"), AuthorizeChecked]
        public ActionResult Detail()
        {
            return Html("/Sys/Organize/Detail");
        }

        /// <summary>
        /// 获取组织机构列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/organize/getListTreeSelect"), LoginChecked]
        public ActionResult GetListTreeSelect()
        {
            var data = organizeLogic.GetList();
            var treeList = new List<TreeSelect>();
            foreach (SysOrganize item in data)
            {
                TreeSelect model = new TreeSelect();
                model.id = item.Id;
                model.text = item.FullName;
                model.parentId = item.ParentId;
                treeList.Add(model);
            }
            return Content(treeList.ToTreeSelectJson());
        }

        /// <summary>
        /// 组织机构主界面数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/organize/index")]
        public ActionResult AppIndex(OrganizeIndexParms parms)
        {
            int totalCount = 0;
            var pageData = organizeLogic.GetList(parms.pageIndex, parms.pageSize, parms.keyWord, ref totalCount);
            var result = new LayPadding<SysOrganize>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount//pageData.Count
            };
            return Content(result.ToJson());
        }

        /// <summary>
        /// 新增/修改组织机构数据提交
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/organize/form")]
        public ActionResult AppForm(SysOrganize model)
        {
            if (model.Id.IsNullOrEmpty())
            {
                int row = organizeLogic.AppInsert(model, model.CreateUser);
                return row > 0 ? Success() : Error();
            }
            else
            {
                int row = organizeLogic.AppUpdate(model, model.ModifyUser);
                return row > 0 ? Success() : Error();
            }
        }

        /// <summary>
        /// 根据主键获取组织数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/organize/getForm")]
        public ActionResult AppGetForm(PrimaryKeyParms parms)
        {
            var entity = organizeLogic.Get(parms.primaryKey);
            return Content(entity.ToJson());
        }

        /// <summary>
        /// 删除组织数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/organize/delete")]
        public ActionResult AppDelete(OrganizeDeleteParms parms)
        {
            int count = organizeLogic.GetChildCount(parms.primaryKey);
            if (count == 0)
            {
                int row = organizeLogic.Delete(parms.primaryKey);
                Logger.OperateInfo($"用户{parms.operateUser}删除了组织机构");
                return row > 0 ? Success() : Error();
            }
            return Error(string.Format("操作失败，请先删除该项的{0}个子级机构。", count));
        }
    }
}
